package com.gui.beans;

/**
 * @author emylyano3
 *
 */

import javax.swing.*;

import java.io.File;
import java.net.URL;

/**
 * Creates a dialog window from where the user will be able to chose a file browsing the file system.
 * */
public abstract class FileSelector extends JPanel {
	
	private static final long serialVersionUID = 1L;
    protected JFileChooser fs;
    
	public FileSelector() {		
		
	}
		
    /** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path) {
    	URL imgURL = FileSelector.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

    /**
     * Create the GUI and show it. 
     */
    public File createAndShowGUI() {
        fs = new JFileChooser();
        fs.setFileSelectionMode(JFileChooser.FILES_ONLY);
        int returnVal = fs.showOpenDialog(FileSelector.this);
        switch (returnVal) {
	        case JFileChooser.APPROVE_OPTION:
	        	return fs.getSelectedFile();
	        case JFileChooser.CANCEL_OPTION:
	        default: 
	        	return null;
        }
    }
	
    protected abstract boolean isCorrectFile(File f);

}
